home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / jpeg / lib / jcmaster.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-15  |  4.9 KB  |  167 lines

  1. /*
  2.  * jcmaster.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains the main control for the JPEG compressor.
  9.  * The system-dependent (user interface) code should call jpeg_compress()
  10.  * after doing appropriate setup of the compress_info_struct parameter.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. METHODDEF void
  17. c_per_scan_method_selection (compress_info_ptr cinfo)
  18. /* Central point for per-scan method selection */
  19. {
  20.   /* Edge expansion */
  21.   jselexpand(cinfo);
  22.   /* Downsampling of pixels */
  23.   jseldownsample(cinfo);
  24.   /* MCU extraction */
  25.   jselcmcu(cinfo);
  26. }
  27.  
  28.  
  29. LOCAL void
  30. c_initial_method_selection (compress_info_ptr cinfo)
  31. /* Central point for initial method selection */
  32. {
  33.   /* Input image reading method selection is already done. */
  34.   /* So is output file header formatting (both are done by user interface). */
  35.  
  36.   /* Gamma and color space conversion */
  37.   jselccolor(cinfo);
  38.   /* Entropy encoding: either Huffman or arithmetic coding. */
  39. #ifdef C_ARITH_CODING_SUPPORTED
  40.   jselcarithmetic(cinfo);
  41. #else
  42.   cinfo->arith_code = FALSE;    /* force Huffman mode */
  43. #endif
  44.   jselchuffman(cinfo);
  45.   /* Pipeline control */
  46.   jselcpipeline(cinfo);
  47.   /* Overall control (that's me!) */
  48.   cinfo->methods->c_per_scan_method_selection = c_per_scan_method_selection;
  49. }
  50.  
  51.  
  52. LOCAL void
  53. initial_setup (compress_info_ptr cinfo)
  54. /* Do computations that are needed before initial method selection */
  55. {
  56.   short ci;
  57.   jpeg_component_info *compptr;
  58.  
  59.   /* Compute maximum sampling factors; check factor validity */
  60.   cinfo->max_h_samp_factor = 1;
  61.   cinfo->max_v_samp_factor = 1;
  62.   for (ci = 0; ci < cinfo->num_components; ci++) {
  63.     compptr = &cinfo->comp_info[ci];
  64.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  65.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  66.       ERREXIT(cinfo->emethods, "Bogus sampling factors");
  67.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  68.                    compptr->h_samp_factor);
  69.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  70.                    compptr->v_samp_factor);
  71.  
  72.   }
  73.  
  74.   /* Compute logical downsampled dimensions of components */
  75.   for (ci = 0; ci < cinfo->num_components; ci++) {
  76.     compptr = &cinfo->comp_info[ci];
  77.     compptr->true_comp_width = (cinfo->image_width * compptr->h_samp_factor
  78.                 + cinfo->max_h_samp_factor - 1)
  79.                 / cinfo->max_h_samp_factor;
  80.     compptr->true_comp_height = (cinfo->image_height * compptr->v_samp_factor
  81.                  + cinfo->max_v_samp_factor - 1)
  82.                  / cinfo->max_v_samp_factor;
  83.   }
  84. }
  85.  
  86.  
  87. /*
  88.  * This is the main entry point to the JPEG compressor.
  89.  */
  90.  
  91.  
  92. GLOBAL void
  93. jpeg_compress (compress_info_ptr cinfo)
  94. {
  95. int    i;
  96.     init_jpeg_header(cinfo);
  97.     i = cinfo->img->frames;
  98.     do {
  99.         if ((*cinfo->img->std_swif)(FI_LOAD_FILE, cinfo->img, 0, 0) < 0)
  100.             break;
  101.         write_next_jpeg_frame(cinfo);
  102.         fprintf(stderr, "%d frames left (%u)\n",
  103.             i, cinfo->img->tmp_offset);
  104.     } while (--i > 0);
  105.     close_jpeg_write(cinfo);
  106. }
  107.  
  108. init_jpeg_header(compress_info_ptr cinfo)
  109. {
  110.  
  111.   /* Read the input file header: determine image size & component count.
  112.    * NOTE: the user interface must have initialized the input_init method
  113.    * pointer (eg, by calling jselrppm) before calling me.
  114.    * The other file reading methods (get_input_row etc.) were probably
  115.    * set at the same time, but could be set up by input_init itself,
  116.    * or by c_ui_method_selection.
  117.    */
  118.   (*cinfo->methods->input_init) (cinfo);
  119.  
  120.   /* Give UI a chance to adjust compression parameters and select */
  121.   /* output file format based on results of input_init. */
  122.   (*cinfo->methods->c_ui_method_selection) (cinfo);
  123.  
  124.   /* Now select methods for compression steps. */
  125.   initial_setup(cinfo);
  126.   c_initial_method_selection(cinfo);
  127.  
  128.   /* Initialize the output file & other modules as needed */
  129.   /* (entropy_encoder is inited by pipeline controller) */
  130.  
  131.   (*cinfo->methods->colorin_init) (cinfo);
  132.   (*cinfo->methods->write_file_header) (cinfo);
  133.  
  134.   single_cc_buf_init(cinfo);
  135.  
  136. #ifdef    STREAM_IMAGE
  137.   {    /* for multi_frame    */
  138.     register int    ci;
  139.     cinfo->comps_in_scan = cinfo->num_components;
  140.     for (ci = 0; ci < cinfo->num_components; ci++)
  141.         cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  142.     (*cinfo->methods->write_scan_header) (cinfo);
  143.   }
  144. #endif
  145. }
  146.  
  147. write_next_jpeg_frame(compress_info_ptr cinfo)
  148. {
  149.   /* Init pass counts to 0 --- total_passes is adjusted in method selection */
  150.   cinfo->total_passes = cinfo->completed_passes = 0;
  151.   (*cinfo->methods->input_term) (cinfo, No);    /* ==    reading_init();    */
  152.   /* And let the pipeline controller do the rest. */
  153.   (*cinfo->methods->c_pipeline_controller) (cinfo);
  154. }
  155.  
  156. close_jpeg_write(compress_info_ptr cinfo)
  157. {
  158.   /* Finish output file, release working storage, etc */
  159.   (*cinfo->methods->write_file_trailer) (cinfo);
  160.   (*cinfo->methods->colorin_term) (cinfo);
  161.   (*cinfo->methods->input_term) (cinfo, Yes);
  162.  
  163.   (*cinfo->emethods->free_all) ();
  164.  
  165.   /* My, that was easy, wasn't it? */
  166. }
  167.